package com.bistu.openlab.uofspreadsheet.xmlparse;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;

import org.dom4j.Document;
import org.dom4j.Element;

import com.bistu.openlab.uofspreadsheet.model.ExcelSheetAtt;
import com.bistu.openlab.uofspreadsheet.util.XmlDomHelper;

/*
 *@author xiaoguang.gao@oracle.com
 *@version 2013-5-9 
 */
public class ExcelSheetAttParse implements IXmlParse<ExcelSheetAtt>{
	private static ExcelSheetAttParse parse = new ExcelSheetAttParse();
	public static ExcelSheetAttParse getInstance(){
		return parse;
	}
	private ExcelSheetAttParse(){}
	
	@Override
	public ExcelSheetAtt readFromElement(Element root) {
		ExcelSheetAtt att = null;
		if(root!=null){
			att = new ExcelSheetAtt();
			att.setElement((Element)root.clone());
		}
		return att;
	}

	@Override
	public Element writeToElement(ExcelSheetAtt obj) {
		Element root = null;
		if(obj==null||obj.getElement()==null){
			URL url = ClassLoader.getSystemClassLoader().getResource("com/bistu/openlab/uofspreadsheet/resource/template/content.xml");
			try {
				URLConnection connection = url.openConnection();
				InputStream stream = connection.getInputStream();
				Document doc = XmlDomHelper.GetDocFromInputStream(stream);	
				if(doc!=null){
					root = (Element)doc.selectSingleNode("表:电子表格文档_E826/表:工作表_E825/表:工作表属性_E80D").clone();
				}
			}catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}else{
			root = obj.getElement();
		}
		return root;
	}

}
